﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.Core;
using Windows.Data.Xml.Dom;
using Windows.Security.Authentication.Identity.Provider;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Notifications;
using MyHelloWinJson;
using MyHelloWinDevice;
using Windows.Devices.Enumeration;
using Windows.Storage;

namespace Tasks
{
    class DeviceMonitor
    {
        CDeviceManger DeviceManager = new CDeviceManger();
        IReadOnlyList<SecondaryAuthenticationFactorInfo> deviceList = null;

        public DeviceMonitor()
        {
            DeviceManager.Added += DeviceManger_Added;
            DeviceManager.Removed += DeviceManger_Removed;
            DeviceManager.Updated += DeviceManger_Update;
            Clear();
        }

        public  void DealWithDevice(string strTaskName, IBackgroundTaskInstance taskInstance)
        {
            UpdateDevice();
            DeviceManager.DealwithDevicesWithTask(strTaskName, taskInstance);
            DeviceManager.VeiwAllDevicesWithTask();
        }

        public async void UpdateDevice()
        {
            deviceList = await SecondaryAuthenticationFactorRegistration.FindAllRegisteredDeviceInfoAsync(SecondaryAuthenticationFactorDeviceFindScope.AllUsers);
        }

        private async void Clear()
        {
            //DebugInfo.ShowToastNotification(args.DeviceID);
            IReadOnlyList<SecondaryAuthenticationFactorInfo> deviceList = await SecondaryAuthenticationFactorRegistration.FindAllRegisteredDeviceInfoAsync(
                    SecondaryAuthenticationFactorDeviceFindScope.AllUsers);

            for (int i = 0; i < deviceList.Count; i++)
            {
                SecondaryAuthenticationFactorInfo deviceInfo = deviceList.ElementAt(i);

                byte[] combinedDataArray;
                CryptographicBuffer.CopyToByteArray(deviceInfo.DeviceConfigurationData, out combinedDataArray);
                string JS = System.Text.Encoding.UTF8.GetString(combinedDataArray);
                JsonData Dat = CMyHelloWinJson.ParesToData(JS);

                if (Dat == null)
                {
                    continue;
                }

                Dat.CanUnLock = false;
                string js = CMyHelloWinJson.ParesToJson(Dat);
                byte[] signArry = System.Text.Encoding.UTF8.GetBytes(js);
                IBuffer deviceConfigData = CryptographicBuffer.CreateFromByteArray(signArry);
                await SecondaryAuthenticationFactorRegistration.UpdateDeviceConfigurationDataAsync(deviceInfo.DeviceId, deviceConfigData);

            }
        }
        private void DeviceManger_Added(CDeviceManger sender, DeviceContext args)
        {
            if (!args.IsConnected)
            {
                DebugInfo.ShowToastNotification("枚举到未连接设备" + args.DeviceID);
                return;
            }
            DebugInfo.ShowToastNotification("枚举到设备" + args.DeviceID);
            for (int i = 0; i < deviceList.Count; i++)
            {
                SecondaryAuthenticationFactorInfo deviceInfo = deviceList.ElementAt(i);

                byte[] combinedDataArray;
                CryptographicBuffer.CopyToByteArray(deviceInfo.DeviceConfigurationData, out combinedDataArray);
                string JS = System.Text.Encoding.UTF8.GetString(combinedDataArray);
                JsonData Dat = CMyHelloWinJson.ParesToData(JS);

                if (Dat == null)
                {
                    continue;
                }

                if (String.Equals(Dat.DeviceID, args.DeviceID))
                {
                    Dat.CanUnLock = true;
                    string js = CMyHelloWinJson.ParesToJson(Dat);
                    byte[] signArry = System.Text.Encoding.UTF8.GetBytes(js);
                    IBuffer deviceConfigData = CryptographicBuffer.CreateFromByteArray(signArry);
                    SecondaryAuthenticationFactorRegistration.UpdateDeviceConfigurationDataAsync(deviceInfo.DeviceId, deviceConfigData);
                    DebugInfo.ShowToastNotification("可使用设备" + Dat.DeviceID + "进行解锁");
                }
            }
        }

        private async void DeviceManger_Removed(CDeviceManger sender, DeviceContext args)
        {
            //DebugInfo.ShowToastNotification(args.DeviceID);
            IReadOnlyList<SecondaryAuthenticationFactorInfo> deviceList = await SecondaryAuthenticationFactorRegistration.FindAllRegisteredDeviceInfoAsync(
                    SecondaryAuthenticationFactorDeviceFindScope.AllUsers);

            for (int i = 0; i < deviceList.Count; i++)
            {
                SecondaryAuthenticationFactorInfo deviceInfo = deviceList.ElementAt(i);

                byte[] combinedDataArray;
                CryptographicBuffer.CopyToByteArray(deviceInfo.DeviceConfigurationData, out combinedDataArray);
                string JS = System.Text.Encoding.UTF8.GetString(combinedDataArray);
                JsonData Dat = CMyHelloWinJson.ParesToData(JS);

                if (Dat == null)
                {
                    continue;
                }

                if (String.Equals(Dat.DeviceID, args.DeviceID))
                {
                    Dat.CanUnLock = false;
                    string js = CMyHelloWinJson.ParesToJson(Dat);
                    byte[] signArry = System.Text.Encoding.UTF8.GetBytes(js);
                    IBuffer deviceConfigData = CryptographicBuffer.CreateFromByteArray(signArry);
                    SecondaryAuthenticationFactorRegistration.UpdateDeviceConfigurationDataAsync(deviceInfo.DeviceId, deviceConfigData);
                    DebugInfo.ShowToastNotification("无法继续使用设备" + Dat.DeviceID + "进行解锁");
                }
            }
        }

        private async void DeviceManger_Update(CDeviceManger sender, DeviceContext args)
        {
            //DebugInfo.ShowToastNotification(args.DeviceID);
            IReadOnlyList<SecondaryAuthenticationFactorInfo> deviceList = await SecondaryAuthenticationFactorRegistration.FindAllRegisteredDeviceInfoAsync(
                    SecondaryAuthenticationFactorDeviceFindScope.AllUsers);

            for (int i = 0; i < deviceList.Count; i++)
            {
                SecondaryAuthenticationFactorInfo deviceInfo = deviceList.ElementAt(i);

                byte[] combinedDataArray;
                CryptographicBuffer.CopyToByteArray(deviceInfo.DeviceConfigurationData, out combinedDataArray);
                string JS = System.Text.Encoding.UTF8.GetString(combinedDataArray);
                JsonData Dat = CMyHelloWinJson.ParesToData(JS);

                if (Dat == null)
                {
                    continue;
                }

                if (String.Equals(Dat.DeviceID, args.DeviceID))
                {
                    Dat.CanUnLock = args.IsConnected;
                    string js = CMyHelloWinJson.ParesToJson(Dat);
                    byte[] signArry = System.Text.Encoding.UTF8.GetBytes(js);
                    IBuffer deviceConfigData = CryptographicBuffer.CreateFromByteArray(signArry);
                    SecondaryAuthenticationFactorRegistration.UpdateDeviceConfigurationDataAsync(deviceInfo.DeviceId, deviceConfigData);

                    if(!Dat.CanUnLock)
                    {
                        DebugInfo.ShowToastNotification("无法继续使用设备" + Dat.DeviceID + "进行解锁");
                    }
                    else
                    {
                        DebugInfo.ShowToastNotification("可使用设备" + Dat.DeviceID + "进行解锁");
                    }
                }
            }
        }
    }
}
